package com.example.hasee.zhcd.Activities.ChuanWu;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by hasee on 2018/5/28.
 */

public class ArrivalsDetailActivity extends BaseActivity {
    private TextView title;
    private ImageView back;
    private ImageView home;
    private TextView one;
    private TextView two;
    private TextView three;
    private TextView four;
    private TextView five;
    private TextView six;
    private TextView seven;
    private TextView eight;
    private TextView nine;
    private TextView ten;
    private TextView eleven;
    private TextView twelve;
    private TextView thirteen;
    private TextView forteen;
    private TextView fifteen;
    private ImageView picture;
    private ScrollView scrollView;
    private Bitmap bitmap;
    private String bmpString;
    private String fshipyearnumvcr;

    @Override
    public int contentViewId() {
        return R.layout.activity_arrivals_detail;
    }

    @Override
    public void set() {
        title.setText("预抵及锚地船舶");
        back.setOnClickListener(this);
        home.setOnClickListener(this);
        scrollView.smoothScrollTo(0, 20);
        scrollView.setFocusable(true);
        picture.setOnClickListener(this);
        fshipyearnumvcr = getIntent().getExtras().getString("fshipyearnumvcr");
        getArrivalsDetail();
    }

    @Override
    public void init() {
        scrollView = (ScrollView) findViewById(R.id.scrollView);
        title = (TextView) findViewById(R.id.centertitle);
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        one = (TextView) findViewById(R.id.one);
        two = (TextView) findViewById(R.id.two);
        three = (TextView) findViewById(R.id.three);
        four = (TextView) findViewById(R.id.four);
        five = (TextView) findViewById(R.id.five);
        six = (TextView) findViewById(R.id.six);
        seven = (TextView) findViewById(R.id.seven);
        eight = (TextView) findViewById(R.id.eight);
        nine = (TextView) findViewById(R.id.nine);
        ten = (TextView) findViewById(R.id.ten);
        eleven = (TextView) findViewById(R.id.eleven);
        twelve = (TextView) findViewById(R.id.twelve);
        thirteen = (TextView) findViewById(R.id.thirteen);
        forteen = (TextView) findViewById(R.id.forteen);
        fifteen = (TextView) findViewById(R.id.fifteen);
        picture = (ImageView) findViewById(R.id.picture);
    }
    private void getArrivalsDetail() {
        ProgressDialogUtils.createProgressDialog(ArrivalsDetailActivity.this,"正在获取数据...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strShipYearNum", fshipyearnumvcr);
        httpUtils.get("YWCX_ydcbDetail", map, "YWCX_ydcbDetail");
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "Fail":
                ProgressDialogUtils.closeProgressDialog();
                Toast.makeText(ArrivalsDetailActivity.this, "操作失败，请检查网络！", Toast.LENGTH_LONG).show();
                break;
            case "YWCX_ydcbDetail":
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONObject data = json.getJSONObject("data");
                        one.setText(data.getString("fberth"));
                        two.setText(data.getString("fshipname"));
                        three.setText(data.getString("fshipordernum"));
                        four.setText(data.getString("fyddate"));
                        five.setText(data.getString("fzygs"));
                        six.setText(data.getString("fdraintimenum"));
                        seven.setText(data.getString("fys"));
                        eight.setText(data.getString("fwxp"));
                        nine.setText(data.getString("fzd"));
                        ten.setText(data.getString("fyb"));
                        eleven.setText(data.getString("fht"));
                        twelve.setText(data.getString("flx"));
                        thirteen.setText(data.getString("fybton"));
                        forteen.setText(data.getString("fpzton"));
                        fifteen.setText(data.getString("fsxbldate"));
                        bmpString = data.getString("fpic");
                        if ("".equals(bmpString)) {
                            picture.setClickable(false);
                        } else {
                            bitmap = base64ToBitmap(data.getString("fpic"));
                            Log.e("图片长度", "" + bmpString.length());
                            picture.setImageBitmap(bitmap);
                        }

                    } else {
                        Toast.makeText(ArrivalsDetailActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    public static Bitmap base64ToBitmap(String base64Data) {
        byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.home:
                GlobalParameter.JumpFrontPage(getApplicationContext());
                break;
            case R.id.picture:
                Intent intent = new Intent(ArrivalsDetailActivity.this, PictrueActivity.class);
                intent.putExtra("string", bmpString);
                startActivity(intent);
                break;
        }
    }
}
